package space.panker916.controller.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import space.panker916.service.DataService;
import space.panker916.utils.HostHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: fake-nowcoder
 * @description: 统计一下 UA 和 DAU
 * @author: PanKer
 * @create: 2022-04-27 20:17
 */
@Component
public class DataInterceptor implements HandlerInterceptor {
    @Autowired
    private DataService dataService;

    @Autowired
    private HostHolder hostHolder;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 统计 UA
        String host = request.getRemoteHost();
        dataService.recordUV(host);

        // 统计 DAU
        if (hostHolder.getUser() != null) dataService.recordDAU(hostHolder.getUser().getId());

        return true;
    }
}
